package com.gl.goods.widght;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gl.goods.R;
import com.gl.goods.base.AppManager;
import com.gl.goods.base.BaseActivity;
import com.gl.library.MaterialProgressBar;


public class LoadingDialog {

	private Dialog loadingDialog;
	private MaterialProgressBar progressBar;
	private BaseActivity mActivity;
	private AppManager manager;

	public LoadingDialog(Context context, int resId) {
		mActivity = (BaseActivity) context;
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view
		LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
		progressBar = (MaterialProgressBar) v.findViewById(R.id.pb_loading);
		TextView loadText = (TextView) v.findViewById(R.id.tv_load_text);
		if (resId != 0) {
			loadText.setVisibility(View.VISIBLE);
			loadText.setText(resId);
		} else {
			loadText.setVisibility(View.GONE);
		}
		loadingDialog = new Dialog(context, R.style.LoadingDialogNoShadow);// 创建自定义样式dialog
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
		loadingDialog.setCanceledOnTouchOutside(false);
		loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局

		/** 监听返回，直接关闭页面 */

		loadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					dismiss();
//					mActivity.superFinish();
				}
				return false;
			}
		});
	}

	/** 设置遮罩显示时是否可以返回 */
	public void setCancelable(boolean cancelable) {
		loadingDialog.setCancelable(cancelable);
		if (!cancelable) {
			loadingDialog.setOnKeyListener(null);
		}

	}

	public void show() {
		loadingDialog.show();
	}

	public void dismiss() {
		if (loadingDialog.isShowing()) {
//			progressBar.seto
			loadingDialog.dismiss();
		}
	}

	public boolean isShowing() {
		return loadingDialog.isShowing();
	}
}
